package com.idbk.chargestation.activity.charge;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonBase;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.Const;
import com.idbk.chargestation.util.GsonUtils;

/**
 * 纠错--联系方式
 * @author lupc
 *
 */
public class ActivityCorrectContact extends BaseActivity implements OnClickListener {

	public final static String TAG = ActivityCorrectContact.class.getSimpleName();

	private Context mContext;

	private int mPointId;

	private TextInputLayout mTextLayout;

	private EditText mEditPhone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_correct_contact);

		setupView();

		setupData();
	}

	private void setupView(){
		mContext = this;

		setupToolBar2();

		mTextLayout = (TextInputLayout)findViewById(R.id.textInput);
		mEditPhone = (EditText)findViewById(R.id.edittext_input);
		
		findViewById(R.id.textview_ok).setOnClickListener(this);
	}

	private void setupData(){
		mPointId = getIntent().getIntExtra(Const.KEY_ID, 0);
	}

	private void submit(){
		if(!checkInput()){
			return;
		}
		showMyProgressDialog(true,true,"提交中...");
		String r = mEditPhone.getText().toString();
		mMainRequest = ChargeStationAPI.correctPointContact(mPointId, r, mResponse);
	}

	private boolean checkInput(){
		hideKeyboard();

		String r = mEditPhone.getText().toString();
		if(r.equals("") || r.length() < 8 || r.length() > 13){
			mTextLayout.setError("请输入正确的联系方式");
			return false;
		}		
		return true;
	}

	private final EHttpResponse mResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonBase result = GsonUtils.toBean(JsonBase.class, response);
			if(handleResponseStatus(result)) {
				Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();
				//提交成功后直接返回到主界面
				finish();				
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			Toast.makeText(mContext, "提交失败", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
			dismissMyProgressDialog();
		}

	};

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.textview_ok){
			submit();
		}
	}

}
